﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale
{
    public partial class 采购材料中标 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            int bid = 0;
            if (!this.IsPostBack)
            {
                this.LeiBie.DataBind();
                this.LeiBie_SelectedIndexChanged(null, null);
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out bid);
                }
                if (bid > 0)
                {
                    this.BID.Text = bid.ToString();
                    this.Button_Info.OnClientClick = "parent.swin2_load('/Sale/采购材料标书录入.aspx?id=" + this.BID.Text + "',600,205);return false;";
                }
                else
                {
                    this.Button_Info.Enabled = false;
                    this.Button_Add.Enabled = false;
                }
            }
            this.SqlDataSource1.SelectParameters["BID"].DefaultValue = this.BID.Text;
            this.Message.Text = "";
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共有 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            decimal shuliang;
            if (!decimal.TryParse(this.ShuLiang.Text, out shuliang))
            {
                this.Message.Text = "“中标数量”录入有误";
                return;
            }
            else if(shuliang<=0)
            {
                this.Message.Text = "“中标数量”必须大于 0";
                return;
            }
            decimal danjia;
            if (!decimal.TryParse(this.DanJia.Text, out danjia))
            {
                this.Message.Text = "“中标单价”录入有误";
                return;
            }
            else if (danjia <= 0)
            {
                this.Message.Text = "“中标单价”必须大于 0";
                return;
            }
            this.SqlDataSource1.InsertParameters["标书ID"].DefaultValue = this.BID.Text;
            this.SqlDataSource1.InsertParameters["材料ID"].DefaultValue = this.LeiBie.Text;
            this.SqlDataSource1.InsertParameters["供货ID"].DefaultValue = this.GongHuo.SelectedItem.Value;
            this.SqlDataSource1.InsertParameters["数量"].DefaultValue = shuliang.ToString();
            this.SqlDataSource1.InsertParameters["单价"].DefaultValue = danjia.ToString();
            this.SqlDataSource1.InsertParameters["备注"].DefaultValue = this.BeiZhu.Text;
            if (this.SqlDataSource1.Insert() > 0)
            {
                this.Message.Text = "增加成功";
            }
            this.ShuLiang.Text = "";
            this.DanJia.Text = "";
            this.BeiZhu.Text = "";

        }

        protected void LeiBie_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DanWei.Text = this.LeiBie.SelectedItem.Text.Split(new string[] { " _ " }, StringSplitOptions.None)[1];
        }
    }
}
